<?php

namespace App\Http\Requests\Admin\Customer\RecommendProduct;

use App\Http\Requests\ApiRequest;
use App\Models\Customer\CustomerRelateHmSkin;
use App\Models\Customer\CustomerRelateWaxSkin;
use App\Models\Store\HmSkinRecord;
use App\Models\Store\WaxSkinRecord;
use App\Services\Presenters\HmSkinData;
use App\Services\Presenters\WaxSkinData;
use Illuminate\Validation\Rule;

/**
 * @property array products
 * @property string skin_type
 * @property array wax_skin
 * @property array hm_skin
 */
class RecommendProductSingleStoreRequest extends ApiRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $hm_item_id = $this->hm_skin['item_id'] ?? HmSkinData::HM_WATER;
        $hm_skin_level_range = HmSkinData::HM_DETECTION_LEVEL_MAP[$hm_item_id];

        $wax_skin_type = class_basename(CustomerRelateWaxSkin::class);
        $hm_skin_type = class_basename(CustomerRelateHmSkin::class);

        return [
            'skin_type'           => 'required|string|in:'. $wax_skin_type . ',' . $hm_skin_type,
            'products'            => 'required|array',
            'products.*'          => 'required|int|min:1',
            'wax_skin'            => 'required_if:skin_type,'.$wax_skin_type,
            'wax_skin.item_id'    => ['required_if:skin_type,'.$wax_skin_type, 'int', Rule::in(WaxSkinRecord::ITEMSID_ALL)],
            'wax_skin.level_sort' => ['required_if:skin_type,'.$wax_skin_type, 'int', Rule::in(WaxSkinData::WAX_DETECTION_LEVEL_MAP)],
            'hm_skin'             => 'required_if:skin_type,'.$hm_skin_type,
            'hm_skin.position_id' => ['required_if:skin_type,'.$hm_skin_type, 'int', Rule::in(HmSkinRecord::ALL_POSITIONS)],
            'hm_skin.item_id'     => ['required_if:skin_type,'.$hm_skin_type, 'int', Rule::in(HmSkinData::HM_ITEM_MAP)],
            'hm_skin.level_sort'  => ['required_if:skin_type,'.$hm_skin_type, 'int', Rule::in($hm_skin_level_range)],
        ];
    }
}
